﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HUGATE.ItemObject;
using HUGATE.Utility;
using CoreLibs;

namespace HUGATE.Item.Controls.ERP.Item.ProductAttribute
{
    public partial class ProductAttributeInput : System.Web.UI.UserControl
    {
        string strProductID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            strProductID = Globals.GetStrRequest("productid", string.Empty);
            if (!IsPostBack)
                LoadData();
        }

        private void LoadData()
        {
            ITEMProductAttribute objProdAttr = new ITEMProductAttribute();
            grvAttributeInput.DataSource = objProdAttr.SearchData(string.Empty);
            grvAttributeInput.DataBind();
        }

        protected void grvAttributeInput_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                int intAttributeType = Convert.ToInt32(drv["AttributeType"]);
                int intAttributeID = Convert.ToInt32(drv["AttributeID"]);
                ITEMProductValue objProdValue = new ITEMProductValue();
                DropDownList ddlValue = e.Row.FindControl("ddlValue") as DropDownList;
                ListBox lsbValue = e.Row.FindControl("lsbValue") as ListBox;
                TextBox txtValue = e.Row.FindControl("txtValue") as TextBox;
                switch (intAttributeType)
                {
                    case 0:
                        InitTextboxData(txtValue, strProductID, intAttributeID);
                        break;
                    case 1:
                        ddlValue.DataSource = objProdValue.GetByAttributeID(intAttributeID);
                        ddlValue.DataBind();
                        ddlValue.Items.Insert(0, new ListItem("[Chọn giá trị]", string.Empty));
                        ddlValue.Visible = true;
                        txtValue.Visible = false;
                        if (!string.IsNullOrEmpty(strProductID))
                            InitDropdownData(ddlValue, strProductID, intAttributeID);
                        break;
                    case 2:
                        lsbValue.DataSource = objProdValue.GetByAttributeID(intAttributeID);
                        lsbValue.DataBind();
                        lsbValue.Items.Insert(0, new ListItem("[Chọn giá trị]", string.Empty));
                        lsbValue.Visible = true;
                        txtValue.Visible = false;
                        if (!string.IsNullOrEmpty(strProductID))
                            InitListboxData(lsbValue, strProductID, intAttributeID);
                        break;
                    default:
                        break;
                }
            }
        }

        private void InitTextboxData(TextBox txtValue, string productID, int attributeID)
        {
            DataTable dtValue = new ITEMProductAttribute().GetByProductAndAttribute(productID, attributeID);
            if (dtValue.Rows.Count > 0)
            {
                txtValue.Text = dtValue.Rows[0]["ValueDescription"].ToString();
            }
        }

        private void InitListboxData(ListBox lsbValue, string productID, int attributeID)
        {
            DataTable dtValue = new ITEMProductAttribute().GetByProductAndAttribute(productID, attributeID);
            foreach (DataRow drw in dtValue.Rows)
            {
                ListItem li = lsbValue.Items.FindByValue(drw["ValueID"].ToString());
                if (li != null)
                    li.Selected = true;
            }
        }

        private void InitDropdownData(DropDownList ddlValue, string productID, int attributeID)
        {
            DataTable dtValue = new ITEMProductAttribute().GetByProductAndAttribute(productID, attributeID);
            if (dtValue.Rows.Count > 0)
            {
                ListItem li = ddlValue.Items.FindByValue(dtValue.Rows[0]["ValueID"].ToString());
                if (li != null)
                    li.Selected = true;
            }
        }
        public string strNewProductID = string.Empty;
        public int SaveData()
        {
            if (string.IsNullOrEmpty(strProductID))
            {
                strProductID = strNewProductID;
            }
            try
            {
                ITEMProductProductAttribute objProdAttrValue = new ITEMProductProductAttribute();
                objProdAttrValue.DeleteByProduct(strProductID);
                foreach (GridViewRow gvrow in grvAttributeInput.Rows)
                {
                    HiddenField hdfAttributeType = gvrow.FindControl("hdfAttributeType") as HiddenField;
                    HiddenField hdfAttributeID = gvrow.FindControl("hdfAttributeID") as HiddenField;
                    TextBox txtValue = gvrow.FindControl("txtValue") as TextBox;
                    DropDownList ddlValue = gvrow.FindControl("ddlValue") as DropDownList;
                    ListBox lsbValue = gvrow.FindControl("lsbValue") as ListBox;
                    objProdAttrValue = new ITEMProductProductAttribute();
                    objProdAttrValue.ProductID = strProductID;
                    objProdAttrValue.AttributeID = hdfAttributeID.Value;
                    switch (Convert.ToInt32(hdfAttributeType.Value))
                    {
                        case 0:
                            if (txtValue.Text.Trim().Length > 0)
                            {
                                objProdAttrValue.ValueID = "0";
                                objProdAttrValue.ValueDescription = txtValue.Text;

                                objProdAttrValue.Insert();
                            }
                            break;
                        case 1:
                            if (ddlValue.SelectedIndex > 0)
                            {
                                objProdAttrValue.ValueID = ddlValue.SelectedValue;
                                objProdAttrValue.ValueDescription = ddlValue.SelectedItem.Text;
                                objProdAttrValue.Insert();
                            }
                            break;
                        case 2:
                            if (!lsbValue.Items[0].Selected)
                            {
                                foreach (ListItem li in lsbValue.Items)
                                {
                                    if (li.Selected)
                                    {
                                        objProdAttrValue.ValueID = li.Value;
                                        objProdAttrValue.ValueDescription = li.Text;
                                        objProdAttrValue.Insert();
                                    }
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception objEx)
            {
                new SystemError("Lỗi khi lưu thuộc tính sản phẩm!", objEx, string.Empty);
                return 0;
            }
            return 1;
        }
    }
}